package tjs.study.notes.dotest.zookper;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Nicky.Tang on 2021/11/10 10:32 上午
 *
 * @since 02.12.10
 */
public class DoTestOfZkClient {
    public static void main(String[] args) throws Exception {
        ZkClient zkClient = new ZkClient("127.0.0.1:2181");
        testTran(zkClient);

        // 新增持久节点
        //zkClient.createPersistent("/a/b/c", true);// true - 递归
        //zkClient.createPersistentSequential("/a/b/c/d1/e1", "d1");// 持久有序节点(必须保证父节点已存在)
        // 新增临时节点(必须保证父节点已存在)
        //zkClient.createEphemeral("/a/b/c/d","e1");
        //zkClient.createEphemeralSequential("/a/b/c/d","e1");

        // 查询节点
        zkClient.subscribeDataChanges("/a/b/cfsgsfdgtds111", new IZkDataListener() {
            @Override
            public void handleDataChange(String dataPath, Object data) throws Exception {
                System.out.println(dataPath + " handleDataChange了..." + data);
            }

            @Override
            public void handleDataDeleted(String dataPath) throws Exception {
                System.out.println(dataPath + " handleDataDeleted了...");
            }
        });
        zkClient.subscribeChildChanges("/a/b/c", new IZkChildListener() {
            @Override
            public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
                System.out.println(parentPath + " handleChildChange了..." + currentChilds.size());
            }
        });

        //zkClient.createEphemeral("/a/b/c/d2", "user");

        while (true) {
            Thread.currentThread().sleep(9999);
        }


//        zkClient.createPersistent(DoTestOfZkLock.curServerPath + "/qq1");
//        String n1 = zkClient.createEphemeralSequential(DoTestOfZkLock.curServerPath + "/qq1/", "t1");
//        String n2 = zkClient.createEphemeralSequential(DoTestOfZkLock.curServerPath + "/qq1/", "t1");
//        zkClient.subscribeDataChanges(n1, new IZkDataListener() {
//            @Override
//            public void handleDataChange(String dataPath, Object data) throws Exception {
//
//            }
//
//            @Override
//            public void handleDataDeleted(String dataPath) throws Exception {
//                System.out.println(dataPath + "诶删除了...");
//            }
//        });
//        zkClient.delete(n1);
//        Thread.currentThread().sleep(300);
//        Thread.currentThread().sleep(300);
//        Thread.currentThread().sleep(300);
    }

    private static void testTran(ZkClient zkClient) {
        if (zkClient.exists("/rolLog/全局唯一事务号1")) {
            zkClient.deleteRecursive("/rolLog/全局唯一事务号1");//内部递归删除
        }
        zkClient.createPersistent("/rolLog/全局唯一事务号1", true);// true - 递归
        zkClient.createPersistent("/rolLog/全局唯一事务号1/微服务A", true);// true - 递归
        zkClient.createPersistent("/rolLog/全局唯一事务号1/微服务B", true);// true - 递归
        zkClient.createPersistent("/rolLog/全局唯一事务号1/微服务C", true);// true - 递归
        zkClient.createPersistentSequential("/rolLog/全局唯一事务号1/微服务B/", "b1");// 持久有序节点(必须保证父节点已存在)
        zkClient.createPersistentSequential("/rolLog/全局唯一事务号1/微服务B/", "b2");// 持久有序节点(必须保证父节点已存在)
        zkClient.createPersistentSequential("/rolLog/全局唯一事务号1/微服务C/", "d1");// 持久有序节点(必须保证父节点已存在)
        zkClient.createPersistentSequential("/rolLog/全局唯一事务号1/微服务C/", "d2");// 持久有序节点(必须保证父节点已存在)
        zkClient.createPersistentSequential("/rolLog/全局唯一事务号1/微服务C/", "d3");// 持久有序节点(必须保证父节点已存在)
        List<String> childrens = zkClient.getChildren("/rolLog/全局唯一事务号1");
        for (String children : childrens) {
            List<String> childrenSorts = zkClient.getChildren("/rolLog/全局唯一事务号1" + "/" + children);
            childrenSorts = childrenSorts.stream().sorted().collect(Collectors.toList());
            for (String childrenSort : childrenSorts) {
                String res = zkClient.readData("/rolLog/全局唯一事务号1" + "/" + children + "/" + childrenSort);
                System.out.println(res);
            }
        }
    }
}
